package com.neuedu.api.collection.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/298:34
 */
public class Register {

    private String name;

    private Date birthday;
    private Integer age;

    private Double salary;

    public Register() {
    }

    public Register(String name, Date birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public Register(String name,  Date birthday,Integer age) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Register(String name, Date birthday, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
        this.salary = salary;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return "Register{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday="  + sdf.format(birthday)  +
                ", salary=" + salary +
                '}';
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


        Register register1  = new Register("张三1" ,sdf.parse("2001-01-01") ,18,99999.0);
        Register register2  = new Register("张三2" ,sdf.parse("2008-01-01") , 25 ,888.0);
        Register register3  = new Register("张三3" ,sdf.parse("2005-01-01") ,33,   7777.0);
        Register register4  = new Register("张三4" ,sdf.parse("2006-01-01") ,17,5555.0);
        Register register5  = new Register("张三5" ,sdf.parse("2003-01-01") ,19,6666.0);


        List<Register> list = new ArrayList<Register>();
        list.add(register1);
        list.add(register2);
        list.add(register3);
        list.add(register4);
        list.add(register5);


        list.forEach(System.out::println);

        //1 使用外部的单独的一个 实现类进行排序规则制定  使用  出生日期进行排序
        RegisterComparator comparator = new RegisterComparator();
        Register maxBirthday = Collections.max(list, comparator);



        System.out.println("maxBirthday = " + maxBirthday);
        // Register min = Collections.min(list, comparator);
        // System.out.println("min = " + min);

    //1 使用外部的单独的一个 实现类进行排序规则制定  使用  年龄排序排序
        InnerRegisterComparator comparator1= new InnerRegisterComparator();
        Register maxAge = Collections.max(list, comparator1);
        System.out.println("maxAge = " + maxAge);


        //获取薪资最低    匿名内部类
       /* Register minSalary = Collections.min(list, new Comparator<Register>() {
                    @Override
                    public int compare(Register o1, Register o2) {
                        //比较的规则
                        return o1.getSalary().compareTo(o2.getSalary());
                    }
            });
        System.out.println("minSalary = " + minSalary);*/



        //Comparator 函数式接口   可以使用 lambda表达式
        // Register minSalary = Collections.min( list,   (Register o1, Register o2)-> {
        //              //比较的规则
        //          return o1.getSalary().compareTo(o2.getSalary());
        // } );
        // System.out.println("minSalary = " + minSalary);




        // Lambda简化的方式 1  参数类型可以省略
        // Register minSalary = Collections.min( list,   ( o1,  o2)-> {
        //     //比较的规则
        //     return o1.getSalary().compareTo(o2.getSalary());
        // } );
        // System.out.println("minSalary = " + minSalary);


        // Lambda简化的方式 2  如果实现过程（大括号中只有一句话 ）  大括号可以省略   ,如果一句话是一个表达式 return  和  末尾的 分好需要去掉
        Register minSalary = Collections.min( list,   ( o1,  o2)->    o1.getSalary().compareTo(o2.getSalary()) );
        System.out.println("minSalary = " + minSalary);

        List<Integer> listInteger  = Arrays.asList(11,22,33,44,55);
        //获取大于 30
        List fiterList = listInteger.stream().filter(new Predicate<Integer>() {
            @Override
            public boolean test(Integer num) {
                return num > 30;
            }
        }).collect(Collectors.toList());

        System.out.println("listInteger = " + listInteger);
        System.out.println("fiterList = " + fiterList);



        List fiterList2 = listInteger.stream().filter(    num->    num % 2==0  ).collect(Collectors.toList());

        System.out.println("fiterList2 = " + fiterList2);


    }

}


//内部类
class InnerRegisterComparator  implements Comparator<Register> {
    @Override
    public int compare(Register o1, Register o2) {
        //比较的规则
        return o1.getAge().compareTo(o2.getAge());
    }
}

